package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.DeviceManage;
import com.woniuxy.mapper.DeviceManageMapper;
import com.woniuxy.service.DeviceManageService;
import com.woniuxy.service.SaasService;
import com.woniuxy.util.PageParam;
import com.woniuxy.util.ResponseData;
import com.woniuxy.util.ResponseEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DeviceManageServiceImpl implements DeviceManageService, SaasService {


    @Resource
    private DeviceManageMapper deviceManageMapper;

    //按照分页加条件查询所有设备
    @Override
    public ResponseData<?> queryDevice(PageParam<DeviceManage> pageParam) {
        PageHelper.startPage(pageParam.getPageNum(),pageParam.getPageSize());
        List<DeviceManage> deviceManageList = deviceManageMapper.queryList(pageParam.getParam());

        return new ResponseData<>().ok(new PageInfo<>(deviceManageList));
    }

    //添加设备
    @Override
    public ResponseData<?> addDevice(DeviceManage deviceManage) {

        if (StringUtils.isBlank(deviceManage.getDeName())||deviceManage.getDePrice()==null ||
            StringUtils.isBlank(String.valueOf(deviceManage.getDePrice()))|| StringUtils.isBlank(String.valueOf(deviceManage.getDeNum()))||
            deviceManage.getDeNum()== null || deviceManage.getDeDepartmentId() == null || StringUtils.isBlank(String.valueOf(deviceManage.getDeDepartmentId()))){
            return new ResponseData<>().fail(ResponseEnum.MANDATORY_EMPTY);
        }


        deviceManageMapper.addDevice(deviceManage);
        return new ResponseData<>().ok();
    }

    //根据id查询设备
    @Override
    public ResponseData<?> queryDeviceById(Integer id) {
        DeviceManage deviceManage = deviceManageMapper.queryDeviceById(id);
        return new ResponseData<>().ok(deviceManage);
    }


    //设备修改
    @Override
    public ResponseData<?> updateDevice(DeviceManage deviceManage) {

        if (StringUtils.isBlank(deviceManage.getDeName())||deviceManage.getDePrice()==null ||
                StringUtils.isBlank(String.valueOf(deviceManage.getDePrice()))|| StringUtils.isBlank(String.valueOf(deviceManage.getDeNum()))||
                deviceManage.getDeNum()== null || deviceManage.getDeDepartmentId() == null || StringUtils.isBlank(String.valueOf(deviceManage.getDeDepartmentId()))){
            return new ResponseData<>().fail(ResponseEnum.MANDATORY_EMPTY);
        }
        deviceManageMapper.updateDevice(deviceManage);
        return new ResponseData<>().ok();
    }

    //删除设备
    @Override
    public ResponseData<?> deleteDevice(Integer id) {
        DeviceManage deviceManage = new DeviceManage();
        deviceManage.setId(id);
        deviceManage.setDeStatus("1");
        deviceManageMapper.updateDevice(deviceManage);
        return new ResponseData<>().ok();
    }
}
